$(function () {

    // 登录 注册 退出
    const token = localStorage.getItem('token')
    const username = localStorage.getItem('username')
    $('.username').html(username)
    if (token) {
        $('.slinks').hide()
        $('.logined').show()
    } else {
        $('.slinks').show()
        $('.logined').hide()
    }

    $('.fa-sign-out').on('click', function () {
        localStorage.removeItem('token')
        localStorage.removeItem('username')
        location.href = '/web-static/index.html'
    })


    // 初始化富文本编辑器
    initEditor()


    const $image = $('#sl')
    function initlist() {
        axios.get('index/index',{
            headers: { token }
    
        }).then(({data:res}) => {
            const htmlStr = template('tpl-list', res)
                $('#tap_list').html(htmlStr)
        })
      
    }
    initlist()

    function initlis() {
        axios.get('/index/index',{
            headers: { token }
    
        }).then(({data:res}) => {
            console.log(res);
                const htmlStr = template('tpl-lis', res)
                $('#tap_lis').html(htmlStr)
        })
       
    }
    initlis()

    // 上传图片
    $('#success').click(function () {
        $('#file').click()
    })

    // 保存文件对象
    const file = null
    const fd = new FormData();
    $('#file').on('change', function (e) {
        const filelists = e.target.files
        if (filelists.length === 0) {
            return file = null
        }
        // 根据文件，创建对应的 URL 地址
        const newImgURL = URL.createObjectURL(filelists[0])
        $image.attr('src', newImgURL)
        file = filelists[0]

        fd.append('file', file)

        $.ajax({
            method: 'POST',
            url: 'http://124.223.14.236:8060/common/upload?type=images',
            headers: {
                token
            },
            data: fd,
            processData: false, // 必须属性
            contentType: false, // 必须属性
            success: function (res) {
                console.log(res);
                file = res.data.savePath
            }
        })
    })



    const art_state = '已发布'
    $('#artSave').on('click', function () {
        art_state = '草稿'
    })

    let author = null
    let cateid = null
    function init() {

        axios.get('/user/getUserInfo',{
            headers: { token }
    
        }).then(({data:res}) => {
            console.log(res);
            author = res.data.userInfo.username
        })
      
    }
    init()

    $('#tap_list').on('change', function () {
        cateid = $(this).val()
    })

    $('.article-form').on('submit', function (e) {
        e.preventDefault()
        fd.append('cateid', cateid)
        fd.append('status', art_state)
        fd.append('pic', file)
        fd.append('title', $('#title').val())
        fd.append('content', $('#content').val().substring(3, $('#content').val().length - 4))
        fd.append('author', author)
        for ( const key of fd.keys()) {
            console.log(key, fd.get(key));
        }
        // 发送Ajax请求, 实现文章发布
        publishArticle(fd)

    })


    function publishArticle(fd) {
        $.ajax({
            method: 'POST',
            url: 'http://124.223.14.236:8060/user/addArticle',
            data: fd,
            headers: {
                token
            },
            processData: false, // 必须属性
            contentType: false, // 必须属性
            success: function (res) {
                if (res.errno !== 0) {
                    return layer.msg('发布文章失败')
                }
                layer.msg('发布文章成功')
                location.href = '/advantages-reach-project/web-static/user-list.html'
            }
        })
    }

    axios.get('/index/index',{
        headers: { token }

    }).then(({data:res}) => {
        if (res.errno !== 0) {
            return console.log('数据请求失败');
        }
        //渲染首页表头列表   
        // let htmlStr1 = template('tpl-nav', res)
        // $('#index-nav').html(htmlStr1)

    })
   

    axios.get('/user/getUserInfo',
    {
        headers: { token }

    }).then(({ data: res }) => {
        console.log(res);
        if (res.errno !== 0) {
             layer.msg('获取用户信息失败')
            location.href = '/web-static/login.html'
        }
    })
})